const express = require("express")
const router = express.Router();//创建路由对象
const { pool, sql } = require("../pool");
// 验证中间件
const expressjoi = require("@escook/express-joi");
// 引入token
const jwt = require("../jwt");


// 引入注册验证规则对象
const { reg_Register_schema } = require("../schema/uRegister")
const { reg_addUbm_schema } = require("../schema/addUbm")
const { reg_onfamilyUbm_schema } = require("../schema/onfamilyUbm")



// 引入md5模块
const md5 = require("../md5");


// 注销登录 未实现
// 登录状态认证
router.post("/news", (req, res) => {
    let { uname, avatar } = req.user;

    res.send({
        ok: 1,
        uname, avatar
    })
})
// 修改密码接口
// 注册接口
//注册或者登录，根据用户名进行验证，有数据就登录，没有就注册
router.post("/ureg", expressjoi(reg_Register_schema),
    (req, res) => { //通过基本验证,进行数据库验证
        let { uname, upwd, remember } = req.body
        sql("SELECT `user`.uname FROM `user` WHERE `user`.uname = ?", uname).then(result => {
            upwd = md5(upwd); //加密密码
            if (result.length) {
                // 用户名存在，转登录 查看密码是否正确

                sql("SELECT `user`.uid,`user`.upwd,`user`.uname,`user`.avatar FROM `user` WHERE `user`.uname = ? AND `user`.upwd = ?", [`${uname}`, `${upwd}`]).then(result => {
                    if (!result.length) {
                        res.send({
                            ok: 0,
                            msg: "密码错误"
                        })
                    } else {
                        // 通过密码校验
                        res.send({
                            status: 200,
                            msg: "登陆成功",
                            remember: remember,
                            token: jwt.generateToken(result[0])
                        })
                    }
                })
            } else {
                // 转注册
                sql("INSERT into user(uname,upwd)values(?,?)", [`${uname}`, `${upwd}`]).then(result => {
                    if (result.affectedRows > 0) {
                        res.send({
                            ok: 1,
                            msg: "注册成功"
                        })
                    } else {
                        res.send({
                            ok: 0,
                            msg: "注册失败"
                        })
                    }
                })
            }

        })

    })
// -----------------------书签路由-----------------------
// 获取用户书签列表 无分类的数据
router.post("/ubmlist", (req, res) => {
    let { uid } = req.user
    sql("SELECT ubm.bmName, ubm.bmurl FROM ubm WHERE ubm.uid = ? AND ubm.fname IS NULL", uid).then(result => {
        if (result.length > 0) {
            res.send({
                ok: 1,
                msg: "获取成功",
                result
            })
        } else {
            res.send({
                ok: 0,
                msg: "这个人一条无分类书签都没有"
            })
        }
    })
})
// 获取分类名
router.post("/ubmfamily", (req, res) => {
    let { uid } = req.user
    // 获取分类
    sql("SELECT ubm.fname FROM ubm WHERE ubm.uid = ?", uid).then(result => {
        if (result.length > 0) {
            let arry = []
            result.forEach((item) => {
                arry.push(item.fname)
            })
            // 去重和过滤
            let farry = [...new Set(arry)].filter(Boolean);
            res.send({
                ok: 1,
                msg: "获取书签分类成功",
                result: farry
            })
        } else {
            res.send({
                ok: 0,
                msg: "这个人没有分类"
            })
        }
    })

})
// 获取分类名称对应的书签列表
router.post("/getubm", expressjoi(reg_onfamilyUbm_schema), (req, res) => {
    let { uid } = req.user
    let { fname } = req.body
    sql("SELECT ubm.bmName, ubm.bmurl FROM ubm WHERE ubm.uid = ? AND ubm.fname = ?", [uid, `${fname}`]).then(result => {
        if (result.length > 0) {
            res.send({
                ok: 1,
                result
            })

        } else {
            res.send({
                ok: 0,
                msg: "无对应分类名"
            })
        }

    })

})
// 用户添加书签
router.post("/addbm", expressjoi(reg_addUbm_schema), (req, res) => {
    // fname可有可无  bmname bmurl必须
    let { uid } = req.user
    let { fname, bmname, bmurl } = req.body
    // 查询是否有过该书签
    sql("SELECT ubm.bmName FROM ubm WHERE ubm.uid = ? AND ubm.bmName = ?", [uid, `${bmname}`]).then(result => {
        if (result.length > 0) {
            // 已经有数据了
            res.send({
                ok: 0,
                msg: "您已经添加过了该书签了"
            })

        } else {
            if (fname) {
                //创建有分类书签
                sql("INSERT into ubm(uid,fname,bmName,bmurl)values(?,?,?,?)", [uid, fname, bmname, bmurl]).then(result => {
                    if (result.affectedRows > 0) {
                        res.send({
                            status: 200,
                            msg: "添加成功"
                        })
                    } else {
                        res.send({
                            status: 200,
                            msg: "添加失败"
                        })
                    }
                })
            } else {
                sql("INSERT into ubm(uid,bmName,bmurl)values(?,?,?)", [uid, bmname, `${bmurl}`]).then(result => {
                    if (result.affectedRows > 0) {
                        res.send({
                            status: 200,
                            msg: "添加成功"
                        })
                    } else {
                        res.send({
                            status: 200,
                            msg: "添加失败"
                        })
                    }
                })
            }


        }
    })


})
module.exports = router;//导出路由